<?php
// Open Header, include functions and get the showid and cached xml
include 'functions.php';
loadheader();
loadletters();
$sid = $_GET['let'];
if(!$sid){$sid = 'a';} 

$cache_time = 3600*168; // 1 week

$cache_file = './cache/'.$sid.'-showlist.xml';
$timedif = @(time() - filemtime($cache_file));

if (file_exists($cache_file) && $timedif < $cache_time) {
    $string = file_get_contents($cache_file);
} else {
    $string = file_get_contents('http://services.tvrage.com/feeds/show_list_letter.php?letter='.$sid);
    if (($string) && ($f = @fopen($cache_file, 'w'))) {
        fwrite ($f, $string, strlen($string));
        fclose($f);
    }
}
// Cached the file, now lets use it.
if (!file_exists($cache_file)) { // Does it exist? If it doesn't there's a problem so lets report it.
 echo "<center><h2>Cache file issue: Cannot open cached XML poss connection issue?</h2></center>";
 break;
}

$xml = simplexml_load_file($cache_file);
if($sid == '#'){ echo "<div class=\"showletter\">Current Complete Show List For Shows Starting With Numbers 1 thru 9</div>\n"; }
else {echo "<div class=\"showletter\">Current Complete Show List For Shows Starting With ".strtoupper($sid)."</div>\n";}
echo "<table border=0 width=600px; cellpadding=2>\n";
echo "<tr><th width=30px;>Show:</th><th width=70px;>Country:</th><th width=80px;>Status:</th></tr>\n";
echo "<tr></tr>\n";
foreach($xml->show as $show){
 		echo "<tr><td><a href=\"eplist.php?sid=".$show->id."\">".$show->name."</a></td>\n";
 		echo "<td>".$show->country."</td>\n";
 		echo "<td>";
 		switch($show->status){
 		 	case 1: echo 'Returning Series'; break;
			case 2: echo 'Canceled/Ended'; break;
			case 3: echo 'TBD/On The Bubble'; break;
			case 4: echo 'In Development'; break;
			case 7: echo 'New Series'; break;
			case 8: echo 'Never Aired'; break;
			case 9: echo 'Final Season'; break;
			case 10: echo 'On Hiatus'; break;
			case 11: echo 'Pilot Ordered'; break;
			case 12: echo 'Pilot Rejected'; break;
 			default: echo 'Unknown'; break;
 		}
 		echo "</td>\n";
 	  echo "</tr>\n";
}

echo "</table>\n";
echo "<p><div align=center>Page generated using a cached feed generated on: ".date ("F d Y H:i:s.", filemtime($cache_file))."</div></p>\n";
loadfooter();
?> 